
#include <File.au3>	

; #CURRENT# ----------------------------------------------
; //_log
; _progressSet()
; transfer_Para_in_File	[]

; getItemValue_Type[]
; getItemValue_Value[]
; getItemValue_Key[]

; strCompare[]
; iniFileCompare[]
; getItemValue_Value_inDestFile[]
; get_compare_Rule
; iniFileCompare_byE[To Do]
;--------------------------------------------------------- 


#cs
;===============================================================================================================
; #Function# ---------------------------------------------
; Name...........: _log
; Description ...: write log in "C:\DSAUTO\log\auto.log"(Default), in folder "log"
; Parameters ....: ($event, $fileName = "auto.log", $filePath = "C:\DSAUTO")
; Return values .: NA
;----------------------------------------------------------
Func _log($event, $fileName = "auto.log", $filePath = "C:\DSAUTO")	;//write to folder log "dsAuto.log"
	$DateNow = @YEAR & "-" & @MON & "-" & @MDAY
	$TimeNow = @HOUR & ":" & @MIN & ":" & @SEC
	If FileExists($filePath & "\log") = 0 Then    
		DirCreate($filePath & "\log")            
	EndIf
	$file = FileOpen($filePath & "\log\" & $fileName, 1) ;Write mode (append to end of file)
	FileWriteLine($file, $DateNow & " " & $TimeNow & " " & $event & @CRLF)
	FileClose($file)	
EndFunc
;===============================================================================================================
#ce

; #Function# ---------------------------------------------
; Name...........: _progressSet($title, $text)
; Description ...: 
; Parameters ....: $title, $text
; Return values .: 
;----------------------------------------------------------

Func _progressSet($title, $text)
	ToolTip(@CRLF & $text & @CRLF & " ", @DesktopWidth - 200, @DesktopHeight - 170, $title,1)	;1= Info icon
	sleep(1000)	
EndFunc




#cs 
;-------------------------------------------------------test_S
	#include <Variable.au3>
	$file			= @ScriptDir & "\config\ExpectedFile\ExpectedFile-Upgrade-3.62.ini"
	$variable_file	= @ScriptDir & "\config\GlobalSetting.ini"
	$output_file    = @ScriptDir & "\config\ExpectedFile\Expected_SS.ini"
	
	transfer_Para_in_File($file,$output_file,$variable_file)	
	
	MsgBox(0,"transfer_Para_in_File","Complete...")		
;-------------------------------------------------------test_E
#ce	


; #Function# ---------------------------------------------
; Name...........: transfer_Para_in_File
; Description ...: transfer expectedfile.ini from $variable_file to outputFile
; Parameters ....: ($file,$variable_file,$variable_SectionName = "Global Variables")
; Return values .: NA
;----------------------------------------------------------

Func transfer_Para_in_File($file,$output_file,$variable_file,$variable_SectionName = "Environment Variable")
	
	_log("Func.au3 transfer_Para_in_File() IN")			
	
	FileCopy ($file, $output_file,1)
	transfor_configure_path()		
	$var = IniReadSection($variable_file, $variable_SectionName)
	;MsgBox(0,"1. $variable_file=",$variable_file)
	;MsgBox(0,"2. $variable_SectionName=",$variable_SectionName)
	
	
	
	If @error Then 
		_log("Func.au3 transfer_Para_in_File(): $variable_file probably Not Found")		
	Else
		For $i = 1 To $var[0][0]
			$key = $var[$i][0]			
			
			$realValue = get_globalvariable_path($key)								
			_ReplaceStringInFile($output_file,"$"&$key,$realValue)			
		Next		
	EndIf	
	_log("Func.au3 transfer_Para_in_File() OUT")
	
EndFunc

;>>>>>>>>>>>>>>>>>>>>>>>>>>>>	[To Do_S]

; #Function# ---------------------------------------------
; Name...........: getItemValue_Value_inDestFile
; Description ...: for section Registry only
; Parameters ....: ($searchFile,$sectionName,$itemValue_Key,$option=0)
;				   0:Default	1:replace policyGUID with *
; Return values .: NA
;----------------------------------------------------------

#cs
;test_S			
	#include<Variable.au3>	
	$searchFile		= @ScriptDir & "\config\sample\Expected_TMPPPPPPPPP.ini"
	$sectionName	= "Registry"
	
	$itemValue_Key	= "DiskMirrors\{4d81577a-e6a7-4580-8815-6befeda3a8b2};ContinuousBuffer;"
	
	$r = getItemValue_Value_inDestFile($searchFile,$sectionName,$itemValue_Key,1)
	MsgBox(0,"Close The search result in dest file: ",$r)
;test_E
#ce

Func getItemValue_Value_inDestFile($searchFile,$sectionName,$itemValue_Key,$option=0)
	
	Local $result
	
	$section_file = IniReadSection($searchFile, $sectionName)	
	$tagFound = 0	
	$k = 1 			
	
	If $sectionName = $TypeAry[3] Then		;Type: Registry		
		Switch $option			
			Case 0
				$itemValue_Key = $itemValue_Key				
			Case 1				
				
				
				$policyGUID = _StringBetween($itemValue_Key, "{", "}");
				
				;MsgBox(0,"$itemValue_Key = ",$itemValue_Key)
				
				$itemValue_Key = StringReplace($itemValue_Key, "\{" & $policyGUID[0] & "}" , "*")
				$itemValue_Key = $RegRootPath & "\" & $itemValue_Key
				
		EndSwitch	
	EndIf


	While $tagFound = 0	And $k <= $section_file[0][0]
		
		$itemValue_KeyDest = getItemValue_Key($sectionName,$section_file[$k][1]) 
		
		If StringCompare($itemValue_Key, $itemValue_KeyDest, 1) = 0 Then									
			$itemValue_Value = getItemValue_Value($sectionName,$section_file[$k][1])
			$result = $itemValue_Value						
		EndIf			
		
		$k = $k + 1			
		
	WEnd
	
	Return $result
	
EndFunc
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>	[To Do_E]


; #Function# ---------------------------------------------
; Name...........: getItemValue_Type
; Description ...: for SD compare result
; Parameters ....: 
;								
; Return values .: NA
;----------------------------------------------------------

#cs
;test_S		
	#include<Variable.au3>	
	$sectionName	= "Registry"
	$itemValue_str	= "HKLM;key;@;#"
	$r = getItemValue_Type($sectionName,$itemValue_str);example
	MsgBox(0,"SSS Test the value of Type: ",$r)
;test_E
#ce

Func getItemValue_Type($sectionName,$itemValue_str)
	
	Local $result	= ""	
	$str = StringSplit($itemValue_str, ";")	
			
	Switch $sectionName					
		Case $TypeAry[0]				;Service
			$index = 2
		Case $TypeAry[1]				;Folder
			$index = 2			
		Case $TypeAry[2]				;File
			$index = 2
		Case $TypeAry[3]				;Registry
			$index = 4			
	EndSwitch			
				
	If $index < UBound($str) Then
		$result = $str[$index]
	EndIf
	
	Return $result	

EndFunc


; #Function# ---------------------------------------------
; Name...........: getItemValue_Value
; Description ...: get section key value of index "$keyValueIndex"
; Parameters ....: ($file,$sectionName,$sectionItemKey,$keyValueIndex,$option=0)
;					$option 0:(default)
;							1:(for SD compared result)
; Return values .: NA
;----------------------------------------------------------

#cs
;test_S	
	#include<Variable.au3>
	
	$sectionName	= "Folder"
	$itemValue_str	= "1"
	$r = getItemValue_Value($sectionName,$itemValue_str);example
	MsgBox(0,"[AAAAA] new keyvalue",$r)

;test_E
#ce

Func getItemValue_Value($sectionName,$itemValue_str,$option=0)
	
	Local $result	= ""	
	$str = StringSplit($itemValue_str, ";")	
	
	
	If $option =0 Then 
			
		Switch $sectionName					
			Case $TypeAry[0]				;Service
				$index = 2
			Case $TypeAry[1]				;Folder
				$index = 2			
			Case $TypeAry[2]				;File
				$index = 3
			Case $TypeAry[3]				;Registry
				$index = 3			
		EndSwitch			
	
	ElseIf $option = 1 Then
		Switch $sectionName					
			Case $TypeAry[0]				;Service
				$index = 1
			Case $TypeAry[1]				;Folder
				$index = 1			
			Case $TypeAry[2]				;File
				$index = 1
			Case $TypeAry[3]				;Registry
				$index = 3			
		EndSwitch			
	
	EndIf
				
	If $index < UBound($str) Then
		$result = $str[$index]
	EndIf
	
	Return $result	

EndFunc


#cs
;test_S

$string1	= "AAA"
$string2	= "BBB"
$equal		= "equal"
$different	= "different"
$r 			= strCompare($string1,$string2,$equal,$different)
MsgBox(0,"",$r)		

;test_E
#ce

; #Function# ---------------------------------------------
; Name...........: strCompare
; Description ...: Compares two strings with case sensitive
; Parameters ....: ($string1,$string2,$equal="#",$different="*")
; Return values .: $equal	    :equal    (default:#)
;                  $different	:different(default:*) 
;----------------------------------------------------------

Func strCompare($string1,$string2,$equal="#",$different="*")
	
	$result = StringCompare ($string1,$string2,1)
	Switch $result
	Case 0							;equal
		Return $equal
	Case Else						;different
		Return $different	
	EndSwitch			
	
EndFunc


#cs
;test_S
	
	#include<Variable.au3>
	$sectionName1	= "Registry"
	$itemValue_str1 	= "regPath;regKey;555;Mark" 
	$r1 = getItemValue_Key($sectionName1,$itemValue_str1)
	MsgBox(0,"Close the Windows",$r1)
	
;test_E
#ce

; #Function# ---------------------------------------------
; Name...........: getItemValue_Key
; Description ...: only for get section Reg
; Parameters ....: ($sectionName,$itemValue_str)
; Return values .: itemValue_Key
;                   
;----------------------------------------------------------

Func getItemValue_Key($sectionName,$itemValue_str)
	
	Local $result	= ""	
	$str = StringSplit($itemValue_str, ";")	
	
	Switch $sectionName					
		Case $TypeAry[0]				;Service
			$index = 1
		Case $TypeAry[1]				;Folder
			$index = 2			
		Case $TypeAry[2]				;File
			$index = 2
		Case $TypeAry[3]				;Registry
			$index = 2			
	EndSwitch	
	
	For $i = 1 To $index				
		$result = $result & $str[$i] &";"	
	Next				
	Return $result	
	
EndFunc


#cs
;test_S
	;#include <Array.au3>
	#include <Variable.au3>
	;#include <log.au3>
	$file1		=	@ScriptDir & "\result\source.ini"
	$file2		=	@ScriptDir & "\result\destination.ini"	
	$resfile	=   @ScriptDir & "\result\SD_rstDDD.ini"
	
	$r = iniFileCompare($file1,$file2,$resfile)
	MsgBox(0,"","iniFileCompare Complete!!")

;test_E
#ce


; #Function# -----------------------------------------------------------------> Not Complete [To Do]
; Name...........: iniFileCompare
; Description ...: only for install,uninstall,upgrade
; Parameters ....: ($file1,$file2,$rstfile)
; Return values .:                    
;----------------------------------------------------------

Func iniFileCompare($file1,$file2,$rstfile)	
		
	_log("iniFileCompare() IN")			
	Local $iniWrtNO1 									;record the number of iniwrite of each section
	Local $iniWrtNO2	
		
	For $i = 0 To UBound($TypeAry)-1					;each section on file1
		$iniWrtNO = 1
		$section_file1 = IniReadSection($file1, $TypeAry[$i])		
		$section_file2 = IniReadSection($file2, $TypeAry[$i])			
		
		;_log("iniFileCompare(), $section_file1[0][0] = " & $section_file1[0][0])
		;_log("iniFileCompare(), $section_file2[0][0] = " & $section_file2[0][0])
		

		;==============================================
		If $TypeAry[$i] <> "Registry" Then
			
			;MsgBox(0,"Type = ",$TypeAry[$i])
			
			If @error Then 
			_log("Error occurred, probably no INI file_1(Func iniFileCompare: " & $file1 & ")")
			Else							
				For $j = 1 To $section_file1[0][0]			;each item of section on file1					
					$item1Value = $section_file1[$j][1]
					$item2Value = $section_file2[$j][1]
					
					If StringCompare($item1Value, $item2Value, 1) = 0 Then							;equal
						IniWrite($rstfile, $TypeAry[$i], $section_file1[$j][0], $item1Value & ";#")
					Else																			;diff				
						IniWrite($rstfile, $TypeAry[$i], $section_file1[$j][0], $item2Value & ";*")
					EndIf
				Next				
			EndIf
			
		ElseIf $TypeAry[$i] = "Registry" Then
			
			If @error Then 
			_log("Error occurred, probably no INI file_2(Func iniFileCompare: " & $file1 & ")")
			Else	
				For $k = 1 To $section_file1[0][0]			;each item of section on file1
					
					;MsgBox(0,"@@ $section_file1[0][0] = ",$section_file1[0][0])

					$item1Value_Key = getItemValue_Key($TypeAry[$i],$section_file1[$k][1])					
					$item1Value 	= getItemValue_Value_inDestFile($file1,$TypeAry[$i],$item1Value_Key,0)
					$item2Value 	= getItemValue_Value_inDestFile($file2,$TypeAry[$i],$item1Value_Key,0)
					
					If StringCompare($item1Value, $item2Value, 1) = 0 Then							;equal
						IniWrite($rstfile, $TypeAry[$i], $section_file1[$k][0], $item1Value_Key & $item1Value & ";#")
					Else																			;diff				
						IniWrite($rstfile, $TypeAry[$i], $section_file1[$k][0], $item1Value_Key & $item2Value & ";*")
					EndIf
				Next
			EndIf
			
		EndIf		
		;==============================================
		
	Next	
	_log("iniFileCompare() OUT")			
EndFunc


#cs
;test_S

#include<Variable.au3>
$action			= 3
$compareType	= "Registry"
$r = get_compare_Rule($action,$compareType)
MsgBox(0,"Rule: " ,$r)

;test_E
#ce

; #Function# ----------------------------------------------
; Name...........: get_compare_Rule($action,$compareType)
; Description ...: 
; Parameters ....: ($action,$compareType)
; Return values .:  1: Rule 1
;					2: Rule 2
;----------------------------------------------------------
Func get_compare_Rule($action,$compareType)

	$value = IniRead($globalvariable_file_name,$Rule_Section,$compareType,"RulePara Not Found")
	
	
	If $value <> $Default_Para_Setting Then
		MsgBox(0,"","user define")
		Return $value		
	Else
		
		;$action = 1 (Install) or 2 (Uninstall) or 3 (Upgrade)				
		
		;--------------------------
		Switch $action					
			Case 1				
				$value = 1			
				
			Case 2
				$value = 1
				
			Case 3
				If $compareType = "Registry" Then
					$value = 2
				Else
					$value = 1
				EndIf			
		EndSwitch
		;--------------------------	
			
		Return $value	


	EndIf	


EndFunc


#cs
;test_S

	#include <Variable.au3>
	
	;$file			= @ScriptDir & "\config\sample\Expected.ini"
	;$variable_file	= @ScriptDir & "\config\sample\GlobalVariables.ini"
	;$newE_file      = @ScriptDir & "\config\sample\Expected_Test.ini"	
	;transfer_Para_in_File($file,$newE_file,$variable_file)	
	
	$file1		= @ScriptDir & "\result\SD_rstDDD.ini"
	$newE_file  = @ScriptDir & "\config\ExpectedFile\Expected_SS.ini"	
	$rstfile	= @ScriptDir & "\result\result_test.ini"
	$reportfile	= @ScriptDir & "\result\report_test.ini"
	
	iniFileCompare_byE($file1,$newE_file,$rstfile,$reportfile)		
	
	MsgBox(0,"","Compare with E Complete")	

;test_E
#ce

; #Function# ----------------------------------------------
; Name...........: iniFileECompare_byE
; Description ...: 
; Parameters ....: 
; Return values .:                    
;----------------------------------------------------------

Func iniFileCompare_byE($file1,$fileE,$rstfile,$reportfile)		
	
	_log("iniFileCompare_byE IN")	
	Local $action
	Local $testResult
	
	$testResult = "---"
	$action = get_action()	;1 (Install) or 2 (Uninstall) or 3 (Upgrade)				
	
	
	For $i = 0 To UBound($TypeAry)-1
		$testResult = "Pass"
		
		$section_file1 	= IniReadSection($file1, $TypeAry[$i])		
		If @error Then 
			_log("iniFileCompare_byE IN, read file1 fail")
		EndIf	
		
		
		$section_fileE 	= IniReadSection($fileE, $TypeAry[$i])
		If @error Then 
			_log("iniFileCompare_byE IN, read fileE fail")
		EndIf	
		
		_log("iniFileCompare_byE(), $TypeAry[$i] = " & $TypeAry[$i])
		_log("iniFileCompare_byE(), $section_file1[0][0] = " & $section_file1[0][0])
		
		
		For $j = 1 To $section_file1[0][0]
			
			;MsgBox(0,"$section_file1[$j][1] = ",$section_file1[$j][1])
       
			$item1Value 	= getItemValue_Value($TypeAry[$i],$section_file1[$j][1],1)
			;_log("iniFileCompare_byE(), get $item1Value finish!!")
			
			$item1Type 		= getItemValue_Type($TypeAry[$i],$section_file1[$j][1])
			;_log("iniFileCompare_byE(), get $item1Type finish!!")
			
			
			If $TypeAry[$i] <> "Registry" Then
				$itemEValue 	= getItemValue_Value($TypeAry[$i],$section_fileE[$j][1],0)
				
			ElseIf $TypeAry[$i] = "Registry" Then		
				$item1Value_Key = getItemValue_Key($TypeAry[$i],$section_file1[$j][1])		
				$itemEValue 	= getItemValue_Value_inDestFile($fileE,$TypeAry[$i],$item1Value_Key,1)
			EndIf
			
			$RULE = get_compare_Rule($action,$TypeAry[$i])
			
			Switch $RULE			
				Case 1
					If $itemEValue = $MARK_DontCare Then
						$compareRst = "Pass"
						
					ElseIf StringCompare($item1Value,$itemEValue,1)=0 Then
						$compareRst = "Pass"
						
					ElseIf StringCompare($item1Value,$itemEValue)<>0 Then
						
						If StringInStr($section_file1[$j][1], ";IMACallbackParams")<>0 Then
							If $item1Value = $itemEValue & " /s" Then
								$compareRst = "Pass"
							EndIf
						EndIf
							
						$compareRst = "Fail"						
						$testResult = "Fail"
						
					EndIf
					
				Case 2
					If $itemEValue = $MARK_DontCare Then
						$compareRst = "Pass"
					
					ElseIf $item1Type = $MARK_Equal Then
						$compareRst = "Pass"
					
					ElseIf $item1Type = $MARK_Diff Then
						If StringCompare($item1Value,$itemEValue,1)=0 Then
							$compareRst = "Pass"
						Else
							$compareRst = "Fail"
							$testResult = "Fail"
						EndIf
						
					EndIf
				
			EndSwitch
			
			If $compareRst <> "Pass" Then
				
				IniWrite($rstfile,$TypeAry[$i],$section_file1[$j][0],$compareRst)
			EndIf			
		Next
	
	IniWrite($Report_file,"Test Result",$TypeAry[$i],$testResult)
	
	Next				
	_log("iniFileCompare_byE OUT")
	
EndFunc

